package com.ymatou.apigateway.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.apigateway.parameter.AesUtil;
import com.ymatou.apigateway.parameter.OpenapiReq;
import com.ymatou.apigateway.parameter.OpenapiService;
import com.ymatou.apigateway.parameter.YmatouPaymentPushBean;
import com.ymatou.apigateway.service.YmatouPaymentPushCall;
import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.database.model.Application;
import com.ymttest.database.model.AuthCode;
import com.ymttest.database.sqlwapper.OpenApiWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/**
 * 订单支付单推送 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_YmatouPaymentPush {
	private static OpenapiReq openapiReq;

	private static YmatouPaymentPushBean ymatouPaymentPushBean;
	private static YmatouPaymentPushCall ymatouPaymentPushCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("推送订单支付单");
	}

	@Before
	public void caseUp() {
		openapiReq = new OpenapiReq();
		openapiReq.setMethod("ymatou.payment.push");

		ymatouPaymentPushBean = new YmatouPaymentPushBean();
		ymatouPaymentPushCall = new YmatouPaymentPushCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(ymatouPaymentPushCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@SuppressWarnings("static-access")
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_YmatouPaymentPush_001() {
		Logger.start(true, "非真实的订单，推送订单支付单");
		try {
			
			TradingServiceResp placeOrder = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.BondedDelivery);

			long userId = placeOrder.getUserId();
			long sellerId = placeOrder.getOrderList().get(0).getSellerId();

			long tradingId = placeOrder.getTradingId();

			long orderId = placeOrder.getOrderList().get(0).getOrderId();

			Logger.comment("前置数据准备结束");
			// 支付订单
			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					tradingId);

			TradingSyncService.getSyncResult(orderId);

			ymatouPaymentPushBean.setOrder_id(orderId);
			
			OpenApiWapper openApiWapper = new OpenApiWapper();

			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(ymatouPaymentPushBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.payment.push",
							app_id);

			ymatouPaymentPushCall.setUrl(url);

			ymatouPaymentPushCall.setData(openapiReq);
			ymatouPaymentPushCall.callService();

			Logger.verifyEquals("0000", ymatouPaymentPushCall.getCode(),
					"验证返回code");
			Logger.verifyEquals(true, ymatouPaymentPushCall.getReturnData()
					.contains("支付信息错误!"), "验证返回内容");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@SuppressWarnings("static-access")
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_YmatouPaymentPush_002() {
		Logger.start(true, "非保税订单，推送订单支付单");
		try {
			
			TradingServiceResp placeOrder = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long userId = placeOrder.getUserId();
			long sellerId = placeOrder.getOrderList().get(0).getSellerId();

			long tradingId = placeOrder.getTradingId();

			long orderId = placeOrder.getOrderList().get(0).getOrderId();

			Logger.comment("前置数据准备结束");
			// 支付订单
			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					tradingId);

			TradingSyncService.getSyncResult(orderId);

			ymatouPaymentPushBean.setOrder_id(orderId);
			
			OpenApiWapper openApiWapper = new OpenApiWapper();

			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(ymatouPaymentPushBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.payment.push",
							app_id);

			ymatouPaymentPushCall.setUrl(url);

			ymatouPaymentPushCall.setData(openapiReq);
			ymatouPaymentPushCall.callService();

			Logger.verifyEquals("0009", ymatouPaymentPushCall.getCode(),
					"验证返回code");
			Logger.verifyEquals(true, ymatouPaymentPushCall.getReturnData()
					.contains("非卖家保税订单!"), "验证返回内容");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@SuppressWarnings("static-access")
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_YmatouPaymentPush_003() {
		Logger.start(true, "非真实的订单，推送订单支付单");
		try {
			
			TradingServiceResp placeOrder = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.BondedDelivery);

			long userId = placeOrder.getUserId();
			long sellerId = placeOrder.getOrderList().get(0).getSellerId();

			long tradingId = placeOrder.getTradingId();

			long orderId = placeOrder.getOrderList().get(0).getOrderId();

			Logger.comment("前置数据准备结束");
			// 支付订单
			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					tradingId);

			TradingSyncService.getSyncResult(orderId);

			ymatouPaymentPushBean.setOrder_id(null);
			
			OpenApiWapper openApiWapper = new OpenApiWapper();

			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(ymatouPaymentPushBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=ymatou.payment.push",
							app_id);

			ymatouPaymentPushCall.setUrl(url);

			ymatouPaymentPushCall.setData(openapiReq);
			ymatouPaymentPushCall.callService();

			Logger.verifyEquals("0001", ymatouPaymentPushCall.getCode(),
					"验证返回code");
			Logger.verifyEquals(true, ymatouPaymentPushCall.getReturnData()
					.contains("orderId:订单ID非法"), "验证返回内容");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}